package com.Glow.reggie.service.impl;

import com.Glow.reggie.dto.DishDto;
import com.Glow.reggie.entity.Dish;
import com.Glow.reggie.entity.DishFlavor;
import com.Glow.reggie.entity.SetmealDish;
import com.Glow.reggie.mapper.DishMapper;
import com.Glow.reggie.service.DishFlavorService;
import com.Glow.reggie.service.DishService;
import com.Glow.reggie.service.SetmealDishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Glow
 * @Date 2022-07-30 21:58:19
 * @Description
 * @Version 1.0
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 新增菜品Dish，同时新增菜品对应的口味需求DishFlavor
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveDishWithDishFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表Dish
        this.save(dishDto);
        Long dishId = dishDto.getId();
        //菜品口味
        List<DishFlavor>flavors = dishDto.getFlavors();
       flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
       dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public void updateDishWithDishFlavor(DishDto dishDto) {
        //更新dish表信息
        this.updateById(dishDto);
        //更新完后删除DishFlavor的口味需求，避免进一步的查询再修改
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);
        //根据提交过来的口味数据再进行插入。
        List<DishFlavor> list = dishDto.getFlavors();
        list = list.stream().map(item->{
                item.setDishId(dishDto.getId());
                return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(list);
    }

    /**
     * 根据id查询菜品信息和对应的菜品口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getDishDtoByIdWithDishFlavor(Long id) {
        //现在Dish表查询菜品信息
        Dish dish = this.getById(id);
        //查询当前菜品对应的
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
        //进行拷贝合并
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    @Transactional
    @Override
    public void remove(String ids) {
        String spilts[] = ids.split(",");
        for(String Id:spilts)
        {
            Long id = Long.valueOf(Id);
            this.removeById(id);
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(queryWrapper);
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getDishId,id);
            setmealDishService.remove(setmealDishLambdaQueryWrapper);

        }
    }

    @Override
    @Transactional
    public void status(Integer status, String ids) {
        String spilts[] = ids.split(",");
        for(String Id:spilts)
        {
            Long id = Long.valueOf(Id);
            Dish dish = this.getById(id);
            dish.setStatus(status);
            this.updateById(dish);
        }
    }
}
